﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using BLL.Services.Friends.Listing;

namespace BLL.Services.Friends
{
    public class FriendsCollection
    {
        private readonly string _connectionString;

        public FriendsCollection(string connectionString)
        {
            _connectionString = connectionString;
        }

        public ListingFriends GetListingFriend(int userId, int page, int size, string column, int direction, ListingFriendsFilter filter)
        {
            if (filter == null)
            {
                filter = new ListingFriendsFilter();
            }

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                var cmd = StoredProcedures.Friends.GetLising(conn, userId, page, size, column, direction, filter.Email, filter.Login);
                var totalCount = new SqlParameter("@TotalCount", SqlDbType.Int, 4)
                    {
                        Direction = ParameterDirection.ReturnValue
                    };
                cmd.Parameters.Add(totalCount);

                var result = new ListingFriends
                    {
                        Column = column,
                        Direction = direction,
                        Filter = filter,
                        Page = page,
                        Size = size
                    };

                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var item = new ListingFriends.Friend
                            {
                                Avatar = reader[0] as string,
                                Email = reader[1] as string,
                                Login = reader[2] as string
                            };
                        result.Friends.Add(item);
                    }
                }

                result.TotalPages = (int)totalCount.Value % size == 0
                                        ? (int)totalCount.Value / size
                                        : (int)totalCount.Value / size + 1;
                return result;
            }

        }

        public void AddFriendToUser(int userId, int friendId, bool isAccepted)
        {
            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                var cmd = StoredProcedures.Friends.Insert(conn, userId, friendId, isAccepted);
                cmd.ExecuteNonQuery();
            }
        }

        public void Update(int userId, int friendid, bool isAccepted)
        {
            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                var cmd = StoredProcedures.Friends.Update(conn, userId, friendid, isAccepted);
                cmd.ExecuteNonQuery();
            }
        }

        public IEnumerable<Friend> FriendByUserId(int userId)
        {
            var result = new List<Friend>();
            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                var cmd = StoredProcedures.Friends.ByUserId(conn, userId);

                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        result.Add(GetFriend(reader));
                    }
                }
            }
            return result;
        }

        public SocialFriends GetFriendsRequests(int userId)
        {
            var result = new SocialFriends();

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();

                var cmd = StoredProcedures.Friends.GetRequest(conn, userId);

                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var friend = new SocialFriends.Friend()
                            {
                                Avatar = reader[0] as string,
                                Login = reader[1] as string,
                                UserId = reader.GetInt32(2)
                            };
                        result.Friends.Add(friend);
                    }
                }
            }
            return result;
        }

        public SocialFriends GetFriendsByUserId(int userId)
        {
            var result = new SocialFriends();

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();

                var cmd = StoredProcedures.Friends.GetByUserId(conn, userId);
                {
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var friend = new SocialFriends.Friend
                                {
                                    Login = reader[0] as string,
                                    Avatar = reader[1] as string
                                };
                            result.Friends.Add(friend);
                        }
                    }
                }
            }
            return result;
        }

        public ListingFriendsIsAccepted GetFriendsListingIsAccepted(int userId, int page, int size, string column, int direction, FriendsIsAcceptedFilter filter)
        {
            if (filter == null)
            {
                filter = new FriendsIsAcceptedFilter();
            }

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();

                var cmd = StoredProcedures.Friends.GetAll(conn, userId, page, size, column, direction, filter.Email, filter.Login);

                var totalCount = new SqlParameter("@TotalCount", SqlDbType.Int, 4)
                    {
                        Direction = ParameterDirection.ReturnValue
                    };
                cmd.Parameters.Add(totalCount);

                var result = new ListingFriendsIsAccepted
                    {
                        Column = column,
                        Direction = direction,
                        Filter = filter,
                        Page = page,
                        Size = size
                    };

                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var friend = new ListingFriendsIsAccepted.Friend
                            {
                                Avatar = reader[0] as string,
                                Email = reader[1] as string,
                                Login = reader[2] as string,
                                FriendId = reader.GetInt32(3)
                            };
                            
                        result.Friends.Add(friend);
                    }
                }

                result.TotalPages = (int)totalCount.Value % size == 0
                                        ? (int)totalCount.Value / size
                                        : (int)totalCount.Value / size + 1;
                return result;
            }
        }
        public void SetFiend(int userId, int friendId)
        {
            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                var cmd = StoredProcedures.Friends.Set(conn, userId, friendId);
                cmd.ExecuteNonQuery();
            }
        }

        public void Delete(int userId, int friendId)
        {
            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                var cmd = StoredProcedures.Friends.Delete(conn, friendId, userId);
                cmd.ExecuteNonQuery();
            }
        }

        private static Friend GetFriend(SqlDataReader reader)
        {
            var friend = new Friend();
            friend.UserId = reader.GetInt32(0);
            friend.Avatar = reader[1] as string;
            friend.Email = reader[2] as string;
            friend.Login = reader[3] as string;
            return friend;
        }


    }
}
